package com.woniu.cq.medical.service.impl;

import com.squirrel.cos.COSAPI;
import com.woniu.cq.medical.common.DateUtils;
import com.woniu.cq.medical.dao.mymapper.My_InformedMapper;
import com.woniu.cq.medical.entity.Informed;
import com.woniu.cq.medical.dao.InformedMapper;
import com.woniu.cq.medical.service.InformedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-09-09
 */
@Service
public class InformedServiceImpl extends ServiceImpl<InformedMapper, Informed> implements InformedService {

    @Autowired
    private My_InformedMapper myInformedMapper;

    @Autowired
    private COSAPI cosapi;

    private static String PATH = "https://klaus-1303116484.cos.ap-chongqing.myqcloud.com/";
    @Override
    public void uploadInformed(MultipartFile file,String uploadname) throws IOException {
        //将文件转存至腾讯云
        //获取输入流
        InputStream inputStream = file.getInputStream();
        //获取流长度
        long size = file.getSize();
        //文件上传
        cosapi.uploadByStream(uploadname,inputStream,null,size);
    }

    /**
     * 插入一张同意书
     */
    @Override
    @Transactional
    public void insertInformed(String filename,String url) {
        //将名字新增进入数据库
        String yyyyMMddHHmmssSSS = DateUtils.format();
        int random = new Random().nextInt(1000);

        Informed informed = new Informed();
        informed.setInformedConsentName(filename);
        informed.setInformedConsentSrc(url);
        myInformedMapper.insertInformed(filename,url);
    }

    @Override
    public List<Informed> selectAllInformed() {
        List<Informed> informeds = myInformedMapper.selectAllInformed();
        return informeds;
    }
}
